/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mlbs.session;

import com.mlbs.entity.Application;
import com.mlbs.entity.Appusage;
import com.mlbs.entity.Usersession;
import com.mlbs.entity.Usertable;
import com.mlbs.helper.Invoice;
import java.util.Collection;
import java.util.Date;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Responsible for providing the billing services in the middleware. Applications can
 * generate invoice or reterive the charges for using a application by any user.
 * 
 * @author hubharan
 */
@PersistenceContext(name="mlbs-ejbPU", unitName="mlbs-ejbPU")
@Stateless
public class BillingSBBean implements BillingSBLocal {
    @PersistenceContext(unitName="mlbs-ejbPU")
    private EntityManager em;
    
    public EntityManager getEntityManager() {
        return em;
    }

    /**
     * Reterive the charges for a given application based on a date range
     * @param appId Application identification
     * @param session session object
     * @param fromDate from date
     * @param toDate end date
     * @return amount
     */
    public float getAppUsageCharges(int appId, String session, Date fromDate, Date toDate) {
        //Get user from session
        Usersession sess = (Usersession)getEntityManager().
                createNamedQuery("Usersession.findBySession").
                setParameter("session", session).
                getSingleResult();
        Usertable user = (Usertable)getEntityManager().
                createNamedQuery("Usertable.findByUserId").
                setParameter("userId", sess.getId()).
                getSingleResult();
        // Query AppUsage table and get all charges for given user and appId,
        // This will return collection of duration, where clause will 
        // search between from and to date
        Collection<Appusage> coll = user.getAppusageCollection();
                
        //Get rate for this appid from preferences
        Application app = (Application)getEntityManager().
                createNamedQuery("Application.findByAppId").
                setParameter("appId", appId).
                getSingleResult();
        Collection appPerf = app.getPreferencesCollection();
        
        // multiply the rate with duration in the collection
        
        //add all the rate * duration and return 
        return 0.0F;
    }

    /**
     * Gets the latest [current month's] invoice for a given user
     * @param session session object
     * @return invoice object
     */
    public Invoice getInvoice(String session) {
        Usersession sess = (Usersession)getEntityManager().
                createNamedQuery("Usersession.findBySession").
                setParameter("session", session).
                getSingleResult();
        Usertable user = (Usertable)getEntityManager().
                createNamedQuery("Usertable.findByUserId").
                setParameter("userId", sess.getId()).
                getSingleResult();
        Collection<com.mlbs.entity.Invoice> invoice = user.getInvoiceCollection();
        //Get invoice for current month and return
        
        return null;
    }

    /**
     * Get all the invoices for a given user for a given date range
     * @param session session object
     * @param fromDate from date
     * @param toDate to date
     * @return array of invoices
     */
    public Invoice [] getInvoices(String session, Date fromDate, Date toDate) {
        Usersession sess = (Usersession)getEntityManager().
                createNamedQuery("Usersession.findBySession").
                setParameter("session", session).
                getSingleResult();
        Usertable user = (Usertable)getEntityManager().
                createNamedQuery("Usertable.findByUserId").
                setParameter("userId", sess.getId()).
                getSingleResult();
        Collection<com.mlbs.entity.Invoice> invoice = user.getInvoiceCollection();
        //Get invoice for current month and return
        return (Invoice [])invoice.toArray();
    }

    /**
     * Update the application usage for a user per application
     * @param appId Application identification
     * @param session session object
     * @param duration duration of the use of the application
     * @return true if update was successful, else false
     */
    public boolean updateUsage(int appId, String session, float duration) {
        Usersession sess = (Usersession)getEntityManager().
                createNamedQuery("Usersession.findBySession").
                setParameter("session", session).
                getSingleResult();
        Usertable user = (Usertable)getEntityManager().
                createNamedQuery("Usertable.findByUserId").
                setParameter("userId", sess.getId()).
                getSingleResult();
        Application app = (Application)getEntityManager().
                createNamedQuery("Application.findByAppId").
                setParameter("appId", appId).
                getSingleResult();
        Appusage usage = new Appusage(new Integer(appId));
        usage.setAppId(app);
        usage.setDateUsed(new Date());
        usage.setDuration(new Integer((int)duration));
        
        usage.setUserId(user);
        em.persist(usage);
        return false;
    }
    
    
    
    
    // Add business logic below. (Right-click in editor and choose
    // "EJB Methods > Add Business Method" or "Web Service > Add Operation")
 

    
    
}
